﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using SYcwpb.Models;
using System.Collections.Specialized;

namespace SYcwpb.ViewModels.DataProvider
{
    //public delegate void ItemEndEditEventHandler(IEditableObject sender);

    public class CrossingRoadObjects : ObservableCollection<CrossingRoadObject>
    {
        public event ItemEndEditEventHandler itemEndEdit;

        public event EventHandler<UpdateArgs> UpdateAverageEvent;  
        
        protected override void InsertItem(int index, CrossingRoadObject item)
        {
            base.InsertItem(index, item);

            item.ItemEndEdit += new ItemEndEditEventHandler(item_ItemEndEdit);
        }

        void item_ItemEndEdit(IEditableObject sender)
        {
            if (itemEndEdit != null)
            {
                itemEndEdit(sender);
            }
        }
        public void NumberSort()
        {
            int i, j;
            CrossingRoadObject index;
            RoadNumberCompare comparer = new RoadNumberCompare();

            for (i = 1; i < this.Count; i++)
            {
                index = Items[i];
                j = i;
                while ((j > 0) && (comparer.Compare(Items[j - 1], index) >= 1))
                {
                    Items[j] = Items[j - 1];
                    j = j - 1;
                }
                Items[j] = index;
            }
            UpdateCollection();
        }
        public void ZBSort() {
            int i, j;
            CrossingRoadObject index;
            ZBRoadsCompare comparer = new ZBRoadsCompare();

            for (i = 1; i < this.Count; i++)
            {
                index = Items[i];
                j = i;
                while ((j > 0) && (comparer.Compare(Items[j - 1], index) >= 1))
                {
                    Items[j] = Items[j - 1];
                    j = j - 1;
                }
                Items[j] = index;
            }

            UpdateCollection();
        }
        public void Sort()
        {
            int i, j;
            CrossingRoadObject index;
            RoadsCompare comparer = new RoadsCompare();

            for (i = 1; i < this.Count; i++)
            {
                index = Items[i];
                j = i;
                while ((j > 0) && (comparer.Compare(Items[j - 1], index) >= 1))
                {
                    Items[j] = Items[j - 1];
                    j = j - 1;
                }
                Items[j] = index;
            }

            UpdateCollection();
            
        }

        public void OnCalculateAverage()
        {
            EventHandler<UpdateArgs> handler = this.UpdateAverageEvent;

            double averageMiles = 0;
            double averageWhours = 0;
            int count = 0;
            for (int i = 0; i < Items.Count;i++ )
            {
                CrossingRoadObject item = Items[i];
                if (!string.IsNullOrEmpty(item.type))
                    continue;

                if (null != item.Miles)
                    averageMiles += Convert.ToDouble(item.Miles);
                if (null != item.Whours)
                    averageWhours += Convert.ToDouble(item.Whours);

                count++;
            }

            if (null != handler)
            {
                UpdateArgs e = new UpdateArgs((averageMiles / count).ToString("0.00"), (averageWhours / count).ToString("0.00"));
                handler(this, e);
            }
        }

        public void UpdateCollection()
        {
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(
                            NotifyCollectionChangedAction.Reset));

            //OnCalculateAverage();
        }
    }

    public class UpdateArgs : EventArgs
    {
        public string AverageMiles = "0";

        public string AverageWhours = "0";

        public UpdateArgs() { }

        public UpdateArgs(string averageMiles,string averageWhours)
        {
            this.AverageMiles = averageMiles;

            this.AverageWhours = averageWhours;
        }
    }
    class RoadNumberCompare : IComparer<CrossingRoadObject>
    {
        public int Compare(CrossingRoadObject x, CrossingRoadObject y)
        {
            int xnumber, ynumber;
            bool xIsNotNumber=false, yIsNotNumber=false;
            xIsNotNumber = !Int32.TryParse(x.ID, out xnumber);
            yIsNotNumber = !Int32.TryParse(y.ID, out ynumber);
            if (xIsNotNumber && yIsNotNumber)
                return Comparer<string>.Default.Compare(x.ID, y.ID);
            else if (xIsNotNumber)
                return 1;
            else if (yIsNotNumber)
                return -1;
            else
                return Comparer<int>.Default.Compare(xnumber, ynumber);
        }
    }
    class ZBRoadsCompare : IComparer<CrossingRoadObject>
    {
        private static Dictionary<string, int> TypeIndex = new Dictionary<string, int>()
        {
            {"",1},
            {"热备",1},
            {"试车线",1},
            {"调车",1},
            {"备班",1},
            {"月备班",6},
            {
                "司机长",7
            },{
                  "督导员",8
              }
        };

        public int Compare(CrossingRoadObject x, CrossingRoadObject y)
        {
            int _index_X = -1;
            int _index_Y = -1;

            TypeIndex.TryGetValue(x.type, out _index_X);
            TypeIndex.TryGetValue(y.type, out _index_Y);

            if (_index_X != _index_Y)
                return _index_X - _index_Y;
            else
            {
                return Comparer<string>.Default.Compare(x.PlanTime, y.PlanTime);
            }
        }

    }
    class RoadsCompare : IComparer<CrossingRoadObject>
    {
        private static Dictionary<string, int> TypeIndex = new Dictionary<string, int>()
        {
            {"",1},
            {"热备",2},
            {"试车线",3},
            {"调车",4},
            {"备班",5},
            {"月备班",6},
            {
                "司机长",7
            },{
                  "督导员",8
              }
        };

        public int Compare(CrossingRoadObject x, CrossingRoadObject y)
        {
            int _index_X = -1;
            int _index_Y = -1;

            RoadsCompare.TypeIndex.TryGetValue(x.type, out _index_X);
            RoadsCompare.TypeIndex.TryGetValue(y.type, out _index_Y);

            if (_index_X != _index_Y)
                return _index_X - _index_Y;
            else
            {
                return Comparer<string>.Default.Compare(x.PlanTime, y.PlanTime);
            }
        }

    }
    public class CrossingRoadObject : IEditableObject, INotifyPropertyChanged
    {
        private CrossingRoadModel _model = CrossingRoadModel.CreateCrossingRoadModel();

        public event ItemEndEditEventHandler ItemEndEdit;

        public void BeginEdit() { }

        public void CancelEdit() { }

        public void EndEdit()
        {
            if (ItemEndEdit != null)
                ItemEndEdit(this);
        }

        public CrossingRoadObject(CrossingRoadModel model)
        {
            this._model = model;
        }

        public CrossingRoadModel GetModel()
        {
            return _model;
        }
        public CrossingRoadObjects CrossingRoadCol { get; set; }

        public string ID
        {
            get { return _model.ID; }
            set
            {
                if (value == _model.ID)
                    return;
                _model.ID = value;
                ////更改ID自动排序，先注释
                //int index;
                //if (CrossingRoadCol != null && Int32.TryParse(_model.ID,out index)&&index<=CrossingRoadCol.Count&&index>0)
                //{
                //    int oldIndex=CrossingRoadCol.IndexOf(this);
                //    int newIndex = Convert.ToInt32(_model.ID) - 1;

                //    if (oldIndex > newIndex)
                //    {
                //        for (int i = newIndex ; i < oldIndex; i++)
                //        {
                //            CrossingRoadCol[i].SetId((Convert.ToInt32(CrossingRoadCol[i]._model.ID) + 1).ToString());
                //        }
                //    }
                //    else
                //    {
                //        for (int i = newIndex; i > oldIndex ; i--)
                //        {
                //            CrossingRoadCol[i].SetId((Convert.ToInt32(CrossingRoadCol[i]._model.ID) - 1).ToString());
                //        }
                //    }

                //    CrossingRoadCol.Move(oldIndex, Convert.ToInt32(_model.ID) - 1);
                //}

                //更新别名
                OnPropertyChanged("ID");
            }
        }
        public string CrName
        {
            get
            {
                return _model.CrName;
            }
            set
            {
                _model.CrName = value;
                OnPropertyChanged("CrName");
            }
        }
        private void SetId(string id)
        {
            this._model.ID = id;
            OnPropertyChanged("ID");
        }
        public string AttenPlace
        {
            get { return _model.AttenPlace; }
            set
            {
                if (value == _model.AttenPlace)
                    return;
                _model.AttenPlace = value;
                OnPropertyChanged("AttenPlace");
            }
        }

        public string PlanTime
        {
            get { return _model.PlanTime; }
            set
            {
                if (value == _model.PlanTime)
                    return;
                _model.PlanTime = value;
                OnPropertyChanged("PlanTime");
            }
        }

        public string TrainTime
        {
            get { return _model.TrainTime; }
            set
            {
                if (value == _model.TrainTime)
                    return;
                _model.TrainTime = value;
                OnPropertyChanged("TrainTime");
            }
        }

        public string RepTrain
        {
            get { return _model.RepTrain; }
            set
            {
                if (value == _model.RepTrain)
                    return;
                _model.RepTrain = value;
                OnPropertyChanged("RepTrain");
            }
        }

        public string StringCrossingRoad
        {
            get { return _model.StringCrossingRoad; }
            set
            {
                if (value == _model.StringCrossingRoad)
                    return;
                _model.StringCrossingRoad = value;
                OnPropertyChanged("StringCrossingRoad");
            }
        }

        public string BCBackGarageTrainID
        {
            get { return _model.BCBackGarageTrainID; }
            set
            {
                if (value == _model.BCBackGarageTrainID)
                    return;

                _model.BCBackGarageTrainID = value;

                OnPropertyChanged("BCBackGarageTrainID");
            }
        }

        public string OffTrain
        {
            get { return _model.OffTrain; }
            set
            {
                if (value == _model.OffTrain)
                    return;
                _model.OffTrain = value;
                OnPropertyChanged("OffTrain");
            }
        }

        public string OffPlace
        {
            get { return _model.OffPlace; }
            set
            {
                if (value == _model.OffPlace)
                    return;
                _model.OffPlace = value;
                OnPropertyChanged("OffPlace");
            }
        }

        public string OffTime
        {
            get { return _model.OffTime; }
            set
            {
                if (value == _model.OffTime)
                    return;
                _model.OffTime = value;
                OnPropertyChanged("OffTime");
            }
        }

        public string Miles
        {
            get { return _model.Miles; }
            set
            {
                if (value == _model.Miles)
                    return;
                _model.Miles = value;
                OnPropertyChanged("Miles");
            }
        }

        public string Whours
        {
            get { return _model.Whours; }
            set
            {
                if (value == _model.Whours)
                    return;
                _model.Whours = value;
                OnPropertyChanged("Whours");
            }
        }

        public string Notes
        {
            get { return _model.Notes; }
            set
            {
                if (value == _model.Notes)
                    return;
                _model.Notes = value;
                OnPropertyChanged("Notes");
            }
        }

        public int midStart
        {
            get { return _model.midStart; }
            set
            {
                if (value == _model.midStart)
                    return;
                _model.midStart = value;
                OnPropertyChanged("midStart");
            }
        }

        public int midOut
        {
            get { return _model.midOut; }
            set
            {
                if (value == _model.midOut)
                    return;
                _model.midOut = value;
                OnPropertyChanged("midOut");
            }
        }

        public string type
        {
            get { return _model.type; }
            set
            {
                if (value == _model.type)
                    return;
                _model.type = value;
                OnPropertyChanged("type");
            }
        }

        public string lastTrainTime
        {
            get { return _model.lastTrainTime; }
            set
            {
                if (value == _model.lastTrainTime)
                    return;
                _model.lastTrainTime = value;
                OnPropertyChanged("lastTrainTime");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (null != handler)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                handler(this, e);
            }
        }
    }
}
